/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Robot;

import lejos.nxt.SensorPort;

/**
 *
 * @author zach
 */
public class SDCLightSensor extends lejos.nxt.SoundSensor{

    private int mean=0;
    private int stdv=100;
    private int num_runs=100;

    public SDCLightSensor(SensorPort port) {
        super(port);
        setBaseLine();
    }
public SDCLightSensor(SensorPort port, boolean floodlight) {
        super(port, floodlight);
        setBaseLine();
    }

    public double getCalabratedValue(){
        return (super.readValue()-mean)/stdv;
    }
    public int getBaseLinedValue(){
        return (super.readValue()-mean);
    }
    private void setBaseLine(){
        int[] zig=new int[num_runs];
        for(int i=0;i<num_runs;i++){
            zig[i]=super.readValue();
            try {
                Thread.sleep(20);
            } catch (InterruptedException ex) {
                //Logger.getLogger(ACLightSensor.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        mean=adv(zig);
        for(int i=0;i<num_runs;i++){
            stdv+=(zig[i]-mean)*(zig[i]-mean);
        }
        stdv=stdv/num_runs;
    }
    private int adv(int[] in){
        int z=0;
        for(int i:in)
            z+=i;
        return z/in.length;
    }
}
